home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / UPC12BS1.ZIP / LIB / EXECUTE.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  26KB  |  787 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       e x e c u t e . C                                            */
  3. /*                                                                    */
  4. /*       Execute an external command for UUPC/extended functions      */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  9. /*       Wonderworks.                                                 */
  10. /*                                                                    */
  11. /*       All rights reserved except those explicitly granted by       */
  12. /*       the UUPC/extended license agreement.                         */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: execute.c 1.11 1993/10/03 22:09:09 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: execute.c $
  24.  * Revision 1.11  1993/10/03  22:09:09  ahd
  25.  * Change debugging messages
  26.  *
  27.  * Revision 1.10  1993/10/02  22:56:59  ahd
  28.  * Suppress compile warning
  29.  *
  30.  * Revision 1.9  1993/10/02  19:07:49  ahd
  31.  * Suppress compiler warning
  32.  *
  33.  * Revision 1.8  1993/09/27  00:45:20  ahd
  34.  * Fix Windows compile, add debug to OS/2 and DOS version
  35.  *
  36.  * Revision 1.7  1993/09/26  03:32:27  dmwatt
  37.  * Use Standard Windows NT error message module
  38.  *
  39.  * Revision 1.6  1993/09/23  03:26:51  ahd
  40.  * Use common file search routine
  41.  *
  42.  * Revision 1.5  1993/09/20  04:38:11  ahd
  43.  * TCP/IP support from Dave Watt
  44.  * 't' protocol support
  45.  * OS/2 2.x support
  46.  *
  47.  * Revision 1.4  1993/08/08  17:39:09  ahd
  48.  * Denormalize path for opening on selected networks
  49.  *
  50.  * Revision 1.3  1993/08/03  03:11:49  ahd
  51.  * Further Windows 3.x fixes
  52.  *
  53.  * Revision 1.2  1993/08/02  03:24:59  ahd
  54.  * Further changes in support of Robert Denny's Windows 3.x support
  55.  *
  56.  * Revision 1.1  1993/07/31  16:22:16  ahd
  57.  * Initial revision
  58.  *
  59.  */
  60.  
  61. /*--------------------------------------------------------------------*/
  62. /*                        System include files                        */
  63. /*--------------------------------------------------------------------*/
  64.  
  65. #include <errno.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #include <ctype.h>
  70. #include <time.h>
  71. #include <process.h>
  72. #include <io.h>
  73.  
  74. #ifdef WIN32
  75. #include <windows.h>
  76. #include <signal.h>
  77. #elif defined(_Windows)
  78. #include <windows.h>
  79. #include <shellapi.h>
  80. #endif
  81.  
  82. #include <direct.h>
  83.  
  84. /*--------------------------------------------------------------------*/
  85. /*                    UUPC/extended include files                     */
  86. /*--------------------------------------------------------------------*/
  87.  
  88. #include "lib.h"
  89. #include "hlib.h"
  90. #include "execute.h"
  91.  
  92. #ifdef _Windows
  93. #include "winutil.h"
  94. #endif
  95.  
  96. #ifdef WIN32
  97. #include "pnterr.h"
  98. #endif
  99.  
  100. /*--------------------------------------------------------------------*/
  101. /*                          Local variables                           */
  102. /*--------------------------------------------------------------------*/
  103.  
  104. currentfile();
  105.  
  106. /*--------------------------------------------------------------------*/
  107. /*                    Internal function prototypes                    */
  108. /*--------------------------------------------------------------------*/
  109.  
  110. static boolean internal( const char *command );
  111.  
  112. static boolean batch( const char *input, char *output);
  113.  
  114. #ifdef _Windows
  115.  
  116. /*--------------------------------------------------------------------*/
  117. /*       e x e c u t e                       (Windows 3.x version)    */
  118. /*                                                                    */
  119. /*       execute external command under Windows                       */
  120. /*--------------------------------------------------------------------*/
  121.  
  122. int execute( const char *command,
  123.              const char *parameters,
  124.              const char *input,
  125.              const char *output,
  126.              const boolean synchronous,
  127.              const boolean foreground )
  128. {
  129.    int result;
  130.  
  131.    boolean useBat = (input != NULL) || (output != NULL );
  132.  
  133.    char path[FILENAME_MAX];         /* String for executable file   */
  134.    char batchFile[FILENAME_MAX];    /* String for batch driver file */
  135.    char perfect[FILENAME_MAX];      /* String for results test file */
  136.  
  137. /*--------------------------------------------------------------------*/
  138. /*                          Locate the command                        */
  139. /*--------------------------------------------------------------------*/
  140.  
  141.    if ( internal( command ) )
  142.    {
  143.       strcpy( path , command );
  144.       useBat = TRUE;
  145.    }
  146.    else if (batch( command, path ))
  147.    {
  148.       if (useBat)                      // Using redirection?
  149.       {
  150.          printmsg(0,"Cannot use redirection with batch file %s",
  151.                      path );
  152.          return -2;
  153.       }
  154.    } /* else */
  155.    else if ( !*path )                  // Error returned from search?
  156.       return -1;                       // Yes --> Error already reported
  157.  
  158. /*--------------------------------------------------------------------*/
  159. /*     Generate a batch file for redirected DOS programs, if needed   */
  160. /*--------------------------------------------------------------------*/
  161.  
  162.    if ( useBat )
  163.    {
  164.       FILE *stream ;
  165.  
  166.       mktempname( batchFile, "BAT");
  167.       mktempname( perfect, "TMP");
  168.       stream = FOPEN( batchFile, "w", TEXT_MODE );
  169.  
  170.       if ( stream == NULL )
  171.       {
  172.          printerr( batchFile );
  173.          panic();
  174.       }
  175.  
  176.       fprintf( stream ,
  177.                "@echo off\n%s %s",
  178.                path,
  179.                parameters == NULL ? "" : parameters );
  180.  
  181.       if ( input != NULL )
  182.          fprintf( stream, " < %s", input );
  183.  
  184.       if ( output != NULL )
  185.          fprintf( stream, " < %s", output );
  186.  
  187.       fprintf( stream,
  188.               "\nif errorlevel 1 erase %s\n",
  189.                perfect );
  190.  
  191.       fclose ( stream );
  192.  
  193.       stream = FOPEN( perfect, "w", TEXT_MODE );
  194.       if ( stream == NULL )
  195.       {
  196.          printerr( perfect );
  197.          panic();
  198.       }
  199.       fclose( stream );
  200.  
  201.       strcpy( path, batchFile );             // Run the batch command
  202.  
  203.    } /* if ( useBat ) */
  204.  
  205. /*--------------------------------------------------------------------*/
  206. /*                       Actually run the command                     */
  207. /*--------------------------------------------------------------------*/
  208.  
  209.    result = SpawnWait( path,
  210.                        parameters,
  211.                        synchronous,
  212.                        foreground ? SW_MAXIMIZE : SW_SHOWMINNOACTIVE );
  213.  
  214. /*--------------------------------------------------------------------*/
  215. /*       For batch files, we can only report zero/non-zero            */
  216. /*       results.  Do so, and clean up our input file at the same     */
  217. /*       time.                                                        */
  218. /*--------------------------------------------------------------------*/
  219.  
  220.    if ( useBat )
  221.    {
  222.       int unlinkResult = unlink( perfect );
  223.  
  224.       if (( result == 0 ) && (unlinkResult != 0))
  225.          result = 255;
  226.  
  227.       if (unlink( batchFile ))
  228.          printerr( batchFile );
  229.  
  230.    } /* if ( useBat ) */
  231.  
  232. /*--------------------------------------------------------------------*/
  233. /*                     Report results of command                      */
  234. /*--------------------------------------------------------------------*/
  235.  
  236.    printmsg( 4,"Result of spawn %s is ... %d", command, result);
  237.  
  238.    return result;
  239.  
  240. } /* execute */
  241.  
  242. #elif defined(WIN32)
  243.  
  244. /*--------------------------------------------------------------------*/
  245. /*    e x e c u t e                             (Windows NT version)  */
  246. /*                                                                    */
  247. /*